package com.mike.employment.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mike.employment.base.BaseDao;
import com.mike.employment.base.BaseServiceImpl;
import com.mike.employment.base.PageRequest;
import com.mike.employment.base.PageResult;
import com.mike.employment.base.exception.BusinessExcep;
import com.mike.employment.dao.NoticeDao;
import com.mike.employment.po.Notice;
import com.mike.employment.service.NoticeService;
import com.mike.employment.utill.NumberUtil;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

@Service
public class NoticeServiceImpl extends BaseServiceImpl<Notice> implements NoticeService{
	
	@Autowired
	private NoticeDao noticeDao;
	
	@PersistenceContext
	private EntityManager entityManager;
	
	@Override
	public BaseDao<Notice> getRepository() {
		return noticeDao;
	}

	@Override
	public EntityManager getEntity() {
		return entityManager;
	}
	
	@Override
	public PageResult getPage(PageRequest req) {
		PageResult<Notice> result = new PageResult<Notice>();
		Specification<Notice> specification  = new Specification<Notice>() {
            
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Notice> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				if (req.getParameter()!=null) {
					req.getParameter().forEach((k,v)->{
					    if("status".equals(k) && v!=null) {  //状态查询
							Predicate p = criteriaBuilder.equal(root.get(k), v);
							predicates.add(p);
					    }else if("title".equals(k) && v!=null && !"".equals(v)) { //公告标题模糊查询
					    	Predicate p = criteriaBuilder.like(root.get(k), "%" + v + "%");
							predicates.add(p);
					    }else if("creater".equals(k) && v!=null) { //创建人外键查询
					    	Predicate p = criteriaBuilder.equal(root.get(k).get("id"), v);
							predicates.add(p);
					    }

					});
				}
				Predicate p = null;
				p = criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
				return p;
			}
		};
		 Pageable pageable = org.springframework.data.domain.PageRequest.of(req.getPageNo()-1, req.getPageSize());
		 try{
			 Page<Notice> page = getRepository().findAll(specification,pageable);
			 result.setTotalPage(page.getTotalPages());
			 result.setTotalCount(page.getTotalElements());
			 result.setPageNo(req.getPageNo());
			 result.setPageSize(req.getPageSize());
			 result.setResult(page.getContent());
			 return result;
		 }catch (Exception e){
		 	throw new BusinessExcep("分页查询失败!", e);
		 }

	}

}
